package com.example.starseas_phone;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;

import com.example.starseas_phone.Adapters.ClassmanageAdapter;
import com.example.starseas_phone.Adapters.CollegemanageAdapter;
import com.example.starseas_phone.Entity.AllClass;
import com.example.starseas_phone.Entity.College;
import com.google.android.material.floatingactionbutton.FloatingActionButton;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class ClassmanageActivity extends AppCompatActivity {

    private FloatingActionButton classmanage_floatbtn;
    private ListView classmanage_list;
    private List<AllClass> list = new ArrayList<>();
    private ClassmanageAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_classmanage);
        initid();
        initfindclass();
        classmanage_floatbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(ClassmanageActivity.this,AddClassActivity.class);
                startActivity(intent);
            }
        });
    }

    private void initid(){
        classmanage_floatbtn = (FloatingActionButton) findViewById(R.id.classmanage_floatbtn);
        classmanage_list = (ListView) findViewById(R.id.classmanage_list);
        adapter = new ClassmanageAdapter(ClassmanageActivity.this,R.layout.collegemanage_listview,list);
    }

    private void initfindclass(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient client = new OkHttpClient();
                    Request request = new Request.Builder()
                            .url(Sto.NetIP+"/api/Class/GetAllClasses")
                            .get()
                            .build();
                    Response response = client.newCall(request).execute();
                    String responseString = response.body().string();
                    JSONObject jsonObject = new JSONObject(responseString);
                    String msg = jsonObject.getString("message");
                    JSONArray jsonArray = new JSONArray(msg);
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jsonObjects = jsonArray.getJSONObject(i);
                        int Id = jsonObjects.getInt("Id");
                        String ClassId = jsonObjects.getString("ClassId");
                        int CollegeId = jsonObjects.getInt("CollegeId");
                        int ProfessionalId = jsonObjects.getInt("ProfessionalId");
                        String Grade = jsonObjects.getString("Grade");
                        String WorkerId = jsonObjects.getString("WorkerId");
                        AllClass allClass = new AllClass(Id,ClassId,CollegeId,ProfessionalId,Grade,WorkerId);
                        list.add(allClass);
                    }
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            adapter.notifyDataSetChanged();
                            classmanage_list.setAdapter(adapter);
                        }
                    });
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }).start();
    }

    @Override
    protected void onRestart() {
        list.clear();
        initfindclass();
        super.onRestart();
    }
}